#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Based on the binutils-avr package

PACKAGE=binutils-pru
TARGET=pru

#ifeq (,$(DEB_BUILD_GNU_TYPE))
#  include /usr/share/dbs/dpkg-arch.mk
#endif

# TODO: Why --disable-static breaks our build? And why AVR guys use it?
CONFARGS = --prefix=/usr \
           --build=$(DEB_BUILD_GNU_TYPE) \
           --host=$(DEB_HOST_GNU_TYPE) \
           --target=$(TARGET) \
           --with-gnu-ld \
           --with-gnu-as \
           --disable-gdb \
           --enable-install-libbfd \
	   --with-bugurl="https://github.com/dinuxbg/gnupru/issues" \
           $(shell dpkg-buildflags --export=configure) 


# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

#TAR_DIR=binutils-*
#TAR_DIR=TAR_DIR=binutils-*
#include /usr/share/dbs/dbs-build.mk
#include debian/dbs-build.mk

BUILD_TREE=src

# the dbs rules
#include /usr/src/toolchain/patches/binutils/dbs-build.mk
#
unpack: unpack-stamp
unpack-stamp:
	ls
	tar xjf binutils-pru_*.tar.bz2
	mv `ls -d binutils* | grep -v tar` src
	#mkdir build
	touch unpack-stamp

patch-stamp: unpack-stamp
	cd $(BUILD_TREE) && for p in `cat ../debian/patchlist`; do echo; echo Applying $$p ...; patch -p1 < ../debian/$$p; done
	touch patch-stamp

configure: configure-stamp
configure-stamp: unpack-stamp patch-stamp
	dh_testdir

	# Add here commands to configure the package.
	#cd $(BUILD_TREE)/ld && aclocal-1.9 && automake-1.9 
	#cd $(BUILD_TREE) && env CC="gcc" CFLAGS="-Wno-error=unused-but-set-variable -Wno-error=unused-but-set-parameter" ./configure $(CONFARGS)
	cd $(BUILD_TREE) && ./configure $(CONFARGS)
	make -C src maybe-configure-bfd
	make -C src/bfd/ headers
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp: configure-stamp
	dh_testdir

	# Add here commands to compile the package.
	cd $(BUILD_TREE) && $(MAKE)
	#/usr/bin/docbook-to-man debian/$(PACKAGE).sgml > $(PACKAGE).1

	touch build-stamp

build-arch: build
build-indep: build	

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp a.out unpack-stamp patch-stamp

	# Add here commands to clean up after the build process.
	#-$(MAKE) clean
	rm -rf $(BUILD_TREE) 
	#rm binutils-2.18.tar.bz2 

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/$(PACKAGE).
	cd $(BUILD_TREE) && $(MAKE) install prefix=$(CURDIR)/debian/$(PACKAGE)/usr

	# Non standard pru dir to keep FHS happy
	mv debian/binutils-pru/usr/pru debian/binutils-pru/usr/lib/

	# Convert hardlinks to softlinks
	cd debian/binutils-pru/usr/lib/pru/bin && for f in *; do \
	    rm ../../../bin/pru-$$f; \
	    ln -s ../lib/pru/bin/$$f ../../../bin/pru-$$f; \
	done

	# Emptying the dependency_libs field of .la files
	sed -i "/dependency_libs/ s/'.*'/''/" `find debian/binutils-pru -name '*.la'`

	ln -s pru-ld.1.gz debian/binutils-pru/usr/share/man/man1/pru-ld.bfd.1.gz

	# Files also in main binutils pkg
	cd debian/$(PACKAGE)/usr && \
	rm -rf lib/libiberty.* lib/libbfd.* lib/libopcodes* \
	include/bfd.h include/ansidecl.h include/bfdlink.h \
	share/locale include/symcat.h include/dis-asm.h \
	info share/info share/gdb include/gdb/jit-reader.h

	mv debian/binutils-pru/usr/$(DEB_HOST_GNU_TYPE)/pru \
	   debian/binutils-pru/usr/lib/pru/$(DEB_HOST_GNU_TYPE)
	rm -r debian/binutils-pru/usr/$(DEB_HOST_GNU_TYPE)

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf	
	dh_installdocs -n
#	dh_installexamples
#	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
	dh_installman
#	dh_installinfo
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps

	# Update toolchain-source version reference (won't work as it's a source dependency)
	#touch debian/substvars
	#-grep -v '^toolchain-source-version=' debian/substvars > debian/substvars.tmp
	#mv debian/substvars.tmp debian/substvars
	#echo >> debian/substvars
	#echo -n 'toolchain-source-version=' >> debian/substvars
	#dpkg -s toolchain-source | grep Version: | sed -e 's/Version: //' >> debian/substvars

	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
